# -*- coding:utf8 -*-
import difflib
import re


def diff_text(a, b):
    s1 = list(a);
    s2 = list(b);
    matcher = difflib.SequenceMatcher(None, s1, s2)
    for tag, i1, i2, j1, j2 in reversed(matcher.get_opcodes()):

        if tag == 'delete':
            # print('Remove {} from positions [{}:{}]'.format(
            #     s1[i1:i2], i1, i2))
            # print('  before =', s1)
            s1[i1:i2] = '***'

        elif tag == 'equal':
            pass
            # print('s1[{}:{}] and s2[{}:{}] are the same'.format(
            #     i1, i2, j1, j2))

        elif tag == 'insert':
            # print('Insert {} from s2[{}:{}] into s1 at {}'.format(
            #     s2[j1:j2], j1, j2, i1))
            # print('  before =', s1)
            s1[i1:i2] = "***"

        elif tag == 'replace':
            # print(('Replace {} from s1[{}:{}] '
            #        'with {} from s2[{}:{}]').format(
            #     s1[i1:i2], i1, i2, s2[j1:j2], j1, j2))
            # print('  before =', s1)
            s1[i1:i2] = "***"
    # print('   after =', s1, '\n')
    # print(a)
    # print(b)
    # print(''.join(s1))
    return ''.join(s1)


def isEnSms(sms):
    return len(sms) == len(bytearray(sms, encoding='utf-8'))


def filter_date_num(sms):
    """
    过滤掉日期时间和数字
    英文短信不过滤
    :param sms:
    :return:
    """
    if isEnSms(sms):
        return sms
    return re.sub(r"[:：\-－\d]{2,}", "***", re.sub(r"[\r\n]+", "***", sms))


def diff_list(test_list):
    if len(test_list) < 2: return None

    # test_list = [  for a in test_list ] #去掉长度大于2的，所有日期、时间、数字
    # test_list = [re.sub('[\d]+', "***", a) for a in test_list]
    # print("test_list")
    # print(test_list)
    last_template = filter_date_num(str(test_list[0]))
    for idx in range(len(test_list) - 1):
        # print(idx)
        last_template = diff_text(last_template, filter_date_num(test_list[idx + 1]))
    # print(last_template)
    # return last_template
    rs = re.sub(r"\*+[ ,，\.。:：、_/★\\-]+|[ ,，\.。:：、_★\\-]+\*+", "***", last_template)
    # rs = re.sub(r"\*+\w|\w\*+", "***", last_template)
    return re.sub(r"\*+", "***", rs)


if __name__ == '__main__':
    a = '尊敬的梅小经\/（女士/先生）：您好，您提交的在北京市第六医院住院的重大疾病互助金申请，缺少： 2021-05-25至2021-06-11住院全套资料原件：1.住院病案首页2.入院记录3.ct报告4.手术记录5.病理报告6.出院记录。请注意！以上资料请补加盖了医院病案室红色印章的原件。因您提供的资料是复印件，不符合要求，请您重新补寄。，为避免影响理赔进度，请尽快将资料补齐后寄至深圳市罗湖区新园路5号工人文化宫职工服务中心一楼6、7号窗口，收件人：深圳市职工保障互助会，电话：0755-82322383，并备注好“姓名+所补材料”。如有疑问，请在工作日上班时间9:00-12:00，下午2:00-6:00联系123123谢谢，顺祝安康。'
    b = '尊敬的何枯\/（女士/先生）：您好，您提交的在深圳市福田区第二人民医院住院的重大疾病互助金申请，缺少： 2021-10-25至2021-11-05住院全套资料原件：1.住院病案首页2.入院记录3.ct报告4.手术记录5.病理报告6.出院记录。请注意！以上资料请补加盖了医院病案室红色印章的原件。因您提供的资料只加盖了骑缝章，不符合要求，请您重新补寄。，为避免影响理赔进度，请尽快将资料补齐后寄至深圳市罗湖区新园路5号工人文化宫职工服务中心一楼6、7号窗口，收件人：深圳市职工保障互助会，电话：0755-82222383，并备注好“姓名+所补材料”。如有疑问，请在工作日上班时间9:00-12:00，下午2:00-6:00联系12312123谢谢，顺祝安康。'

    print(diff_text(a, b))
    print(diff_list([a, b]))

    a = '【酒店】为了给我们的客人一个更好的入住体验https://csccworkorder.byd.com/234asdf/asdf，请今天的MOD—Candy Dai 于18：00-19：30期间在48楼大堂与住店和抵店的客人进行交流与迎接。In order to give our guests a better experience of the St. Regis, today\'s MOD—Candy Dai will have a great engagement with the in house and the arrival guest at 18：00-19：30 on the 48th floor lobby.'
    b = '【酒店】为了给我们的客人一个更好的入住体验https://csccworkorder.byd.com/123123，请今天的MOD—Candy Dai 于18：00-19：30期间在48楼大堂与住店和抵店的客人进行交流与迎接。In order to give our guests a better experience of the St. Regis, today\'s MOD—Candy Dai will have a great engagement with the in house and the arrival guest at 18：00-19：30 on the 48th floor lobby.'

    print(diff_list([a, b]))
    a = "[TWLNET]We noticed you may not be travel ready1223 for your trip to Shanghai. To make your upcoming travel to Shanghai as smooth as possible, visit our Travel-Ready Center prior to check-in to upload your travel documents and have them approved prior to your trip. Doing so will help speed up your airport experience. If you've already uploaded your documents and they're still pending, we'll let you know when they're approved. Travel-Ready Center: https://fly.united.com/Sgpckqi Please be sure to check all of your travel documents are valid and up-to-date. Confirmation number: DT1YH5"
    b = "[TWLNET]We noticed you may not be travel ready1231 for your trip to Shanghai. To make your upcoming travel to Shanghai as smooth as possible, visit our Travel-Ready Center prior to check-in to upload your travel documents and have them approved prior to your trip. Doing so will help speed up your airport experience. If you've already uploaded your documents and they're still pending, we'll let you know when they're approved. Travel-Ready Center: https://fly.united.com/U3pckpn Please be sure to check all of your travel documents are valid and up-to-date. Confirmation number: B716LF"
    print(diff_list([a, b]))

    test_list = [
        '【人民医院】陈试坤女士\/，您好！您在我院进行的检验项目：新型冠状病毒核酸(RNA)检测，结果：阴性,报告时间:2022-03-10 23:27:26.123/\，凭此登记号0005898598到自助机打印报告。如需咨询医生，请登录我院微信公众号–互联网医院–在线诊疗。',
        '【人民医院】田林坤先生\/，您好！您在我院进行的检验项目：新型冠状病毒核酸(RNA)检测，结果：阴性,报告时间:2022-03-10 23:29:34，凭此登记号0005444058到自助机打印报告。如需咨询医生，请登录我院微信公众号–互联网医院–在线诊疗。',
        '【人民医院】吴莲娣女士\/，您好！您在我院进行的检验项目：新型冠状病毒核酸(RNA)检测，结果：阴性,报告时间:2022-03-10 23:27:10，凭此登记号0000447365到自助机打印报告。如需咨询医生，请登录我院微信公众号–互联网医院–在线诊疗。',
        '【人民医院】吴莲娣女士\/，您好！您在我院进行的检验项目：新型冠状病毒核酸(RNA)检测，结果：阴性,报告时间:2022-03-10 23:27:16，凭此登记号0000447365到自助机打印报告。如需咨询医生，请登录我院微信公众号–互联网医院–在线诊疗。',
        '【人民医院】林翊女士\/，您好！您在我院进行的检验项目：新型冠状病毒核酸(RNA)检测，结果：阴性,报告时间:2022-03-10 23:27:10，凭此登记号0002821499到自助机打印报告。如需咨询医生，请登录我院微信公众号–互联网医院–在线诊疗。',
        '【人民医院】苏晓丹女士\/，您好！您在我院进行的检验项目：新型冠状病毒核酸(RNA)检测，结果：阴性,报告时间:2022-03-10 23:27:23，凭此登记号0020342808到自助机打印报告。如需咨询医生，请登录我院微信公众号–互联网医院–在线诊疗。'
    ]
    print(diff_list(test_list))

    x=[5,2,3,4]
    y=[1,6,7,8]
    c = [y, x][x < y]
    print(c) #[5, 2, 3, 4]